<?php
namespace Home\Controller;
use Think\Controller;
use Think\Db;
class ByController extends Controller{
	public function index(){
	if(isWeixinBrowser()){
		if(IS_POST){
			$code=preg_replace('/\s+/','',$_POST['code']);
			$codeDb=M('code','by_');
			$codeResult=$codeDb->where("code='%s'",$code)->find();
			if(is_array($codeResult)){
				if($codeResult['times']==1){
					$codeResult['times']=0;
					$codeDb->save($codeResult);			
					$prizeDb=M('prize','by_');
					$prize_arr=$prizeDb->where('id>0')->select();
					$this->assign('prize_arr',$prize_arr);
					$prize=getPrize($prize_arr);
					$this->assign('prize',$prize);
					$this->assign('code',$code);
					$this->display('by');
					$id=$prize['id'];
					$prize_arr[$id-1]['v']=$prize_arr[$id-1]['v']-1;
					$prizeDb->save($prize_arr[$id-1]);	
				}
				else{
					$this->assign('content','您输入的抽奖码已经被使用，请不要重复使用');
					$this->display('warning');
				}
			}
			else {
				$this->assign('content','您输入的抽奖码不正确，请核对后重新输入');
				$this->display('warning');
			}
		}
		else{
			$this->assign('content','您尚未输入抽奖码,暂无权限进行抽奖');
			$this->display('warning');
		}
		}
		else{
			echo '请用微信打开';
		}
	}
	public function login(){
		if(isWeixinBrowser()){
			$this->display();
		}
		else{
			echo '请用微信打开';
		}
	}
	public function admin($username=null,$password=null){
		if(IS_POST){
			if($username=='by'&&$password=='bywhl'){
				$this->display('admin');
				$this->setPrize();
			}
			else{
				echo '用户名或者密码错误';
			}
		}
		else{
			$this->display('loginadmin');
		}
	} 
	private function setPrize(){
		$dbconfig['DB_TYPE']=C('DB_TYPE');
		$dbconfig['DB_HOST']=C('DB_HOST');
		$dbconfig['DB_NAME']=C('DB_NAME');
		$dbconfig['DB_USER']=C('DB_USER');
		$dbconfig['DB_PWD']=C('DB_PWD');
		$dbconfig['DB_PORT']=C('DB_PORT');
		$dbconfig['DB_PREFIX']=by_;
		$dbconfig['DB_CHARSET']=C('DB_CHARSET');
		//print_r($dbconfig);
		$db=Db::getInstance($dbconfig);
		create_tables($db);
		
		/*$prize_arr = array(
		 '0' => array('id' =>1,'name' =>'一等奖','content'=>'现金50元','num'=>50,'v' =>2),
		 '1' => array('id' =>2,'name' =>'二等奖','content'=>'现金40元','num'=>40,'v' =>5),
		 '2' => array('id' =>3,'name' =>'三等奖','content'=>'现金30元','num'=>30,'v' =>15),
		 '3' => array('id' =>4,'name' =>'四等奖','content'=>'现金20元','num'=>20,'v' =>18),
		 '4' => array('id' =>5,'name' =>'五等奖','content'=>'现金10元','num'=>10,'v' =>139)
		);
		foreach ($prize_arr as $key => $value) {
			M('prize','by_')->data($value)->add();
		}*/
		//测试username数据库
		/*$formusername=$_GET['fromusername']?$_GET['fromusername']:'test';
		$data=M('user')->where("fromusername='%s'",$formusername)->find();
		$scratchDb=M('scratch');
		$result=$scratchDb->where("username='%s'",$data['username'])->find();
		if(!$result){
			$scratch=array('username'=>$data('username'),
					'times'=>2000,
					'time'=>time(),
			);
			$scratchDb->data($scratch)->add();
		}else{
			$result['times']=2000;
			$result['time']=time();
			$scratchDb->save($result);
		}
		echo $formusername."抽奖次数2000";*/
	}
	private function setCode() {
		/*$codeDb=M('code','by_');
		$codeResult=array();
		for($i=0;$i<300;$i++){
			do{
				$code=strtoupper(substr(md5(time()),mt_rand(1,25),6));
				$codeResult=$codeDb->where("code='%s'",$code)->find();
			}
			while((strstr($code,"E")!=false)||is_array($codeResult));
			$code_arr['code']=$code;
			$code_arr['times']=1;
			$codeDb->data($code_arr)->add();
		}
		echo '成功';*/
	}
}
